package com.crm.business.crm.controller;


import com.crm.business.crm.log.CrmLeadsLog;
import com.crm.business.crm.service.CrmUploadExcelService;
import com.crm.business.crm.service.ICrmAgreementService;
import com.crm.business.utils.AuthUtil;
import com.crm.business.utils.UserUtil;
import com.crm.common.crm.common.CrmModel;
import com.crm.common.crm.constant.CrmCodeEnum;
import com.crm.common.crm.constant.CrmEnum;
import com.crm.common.crm.constant.FieldEnum;
import com.crm.common.crm.entity.BO.*;
import com.crm.common.crm.entity.PO.CrmAgreement;
import com.crm.common.crm.entity.PO.CrmLeads;
import com.crm.common.crm.entity.PO.CrmTmkLeads;
import com.crm.common.crm.entity.VO.CrmDataCheckVO;
import com.crm.common.crm.entity.VO.CrmInfoNumVO;
import com.crm.common.crm.entity.VO.CrmLeadsTransferCustomer;
import com.crm.common.crm.entity.VO.CrmModelFiledVO;
import com.crm.common.ka.core.common.R;
import com.crm.common.ka.core.common.Result;
import com.crm.common.ka.core.common.SubModelType;
import com.crm.common.ka.core.common.SystemCodeEnum;
import com.crm.common.ka.core.common.log.BehaviorEnum;
import com.crm.common.ka.core.common.log.SysLog;
import com.crm.common.ka.core.common.log.SysLogHandler;
import com.crm.common.ka.core.entity.BasePage;
import com.crm.common.ka.core.exception.CrmException;
import com.crm.common.ka.core.servlet.upload.FileEntity;
import com.crm.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/crmAgreement")
@Api(tags = "协议模块接口")
public class CrmAgreementController {


    @Autowired
    private ICrmAgreementService crmAgreementService;

    @Autowired
    private CrmUploadExcelService uploadExcelService;



    @PostMapping("/queryById/{agreementId}")
    @ApiOperation("根据ID查询")
    public Result<CrmModel> queryById(@PathVariable("agreementId") @ApiParam(name = "id", value = "id") Integer agreementId) {
        Integer number = crmAgreementService.lambdaQuery().eq(CrmAgreement::getAgreementId, agreementId).count();
        if (number == 0) {
            throw new CrmException(CrmCodeEnum.CRM_DATA_DELETED, "线索");
        }
        CrmModel model = crmAgreementService.queryById(agreementId);
        return R.ok(model);
    }

    @PostMapping("/allExportExcel")
    @ApiOperation("全部导出")
    @SysLogHandler(behavior = BehaviorEnum.EXCEL_EXPORT, object = "线索导出", detail = "全部导出")
    public void allExportExcel(@RequestBody CrmSearchBO search, HttpServletResponse response) {
        search.setPageType(0);
        List<Integer> list = UserUtil.getUser().getRoles();
        Integer superRole = UserUtil.getSuperRole();
        boolean b = list.contains(superRole);
        if (b){
            search.setPageType(10000);
        }
        crmAgreementService.exportExcel(response, search);
    }

    @PostMapping("/batchExportExcel")
    @ApiOperation("选中导出")
    @SysLogHandler(behavior = BehaviorEnum.EXCEL_EXPORT, object = "线索导出", detail = "选中导出")
    public void batchExportExcel(@RequestBody @ApiParam(name = "ids", value = "id列表") List<Integer> ids, HttpServletResponse response) {
        CrmSearchBO search = new CrmSearchBO();
        search.setPageType(0);
        Integer superRole = UserUtil.getSuperRole();
        List<Integer> list = UserUtil.getUser().getRoles();
        boolean b = list.contains(superRole);
        if (b){
            search.setPageType(10000);
        }
        search.setLabel(CrmEnum.AGREEMENT.getType());
        CrmSearchBO.Search entity = new CrmSearchBO.Search();
        entity.setFormType(FieldEnum.TEXT.getFormType());
        entity.setSearchEnum(CrmSearchBO.FieldSearchEnum.ID);
        entity.setValues(ids.stream().map(Object::toString).collect(Collectors.toList()));
        search.getSearchList().add(entity);
        search.setPageType(0);
        if (b){
            search.setPageType(10000);
        }
        crmAgreementService.exportExcel(response, search);
    }

    @PostMapping("/queryPageList")
    @ApiOperation("查询列表页数据")
    public Result<BasePage<Map<String, Object>>> queryPageList(@RequestBody CrmSearchBO search) {
        BasePage<Map<String, Object>> mapBasePage = crmAgreementService.queryPageList(search);
        return R.ok(mapBasePage);
    }


    @PostMapping("/field")
    @ApiOperation("查询新增所需字段")
    public Result<List<CrmModelFiledVO>> queryField() {
        List<CrmModelFiledVO> crmModelFiledList = crmAgreementService.queryField(null);
        return R.ok(crmModelFiledList);
    }

    @PostMapping("/field/{id}")
    @ApiOperation("查询修改数据所需信息")
    public Result<List<CrmModelFiledVO>> queryField(@PathVariable("id") @ApiParam(name = "id", value = "id") Integer id) {
        List<CrmModelFiledVO> crmModelFiledList = crmAgreementService.queryField(id);
        return R.ok(crmModelFiledList);
    }

    @PostMapping("/add")
    @ApiOperation("保存数据")
    public Result add(@RequestBody CrmAgreementSaveBO crmModel) {
        Map<String,String> result = crmAgreementService.addOrUpdate(crmModel,false);
        return R.ok(result);
    }

    @GetMapping("/canSubmit")
    @ApiOperation("是否可发起签署")
    public Result canSubmit() {
        boolean b = crmAgreementService.canSubmit();
        Map<String,Object> result = new HashMap<>();
        result.put("result",b);
        return R.ok(result);
    }


    @PostMapping("/queryFileList")
    @ApiOperation("查询附件列表")
    public Result<List<FileEntity>> queryFileList(@RequestParam("id") @ApiParam(name = "id", value = "id") Integer id) {
        List<FileEntity> fileEntities = crmAgreementService.queryFileList(id);
        return R.ok(fileEntities);
    }

    @PostMapping("/num")
    @ApiOperation("详情页数量展示")
    public Result<CrmInfoNumVO> num(@RequestParam("id") @ApiParam(name = "id", value = "id") Integer id) {
        CrmInfoNumVO num = crmAgreementService.num(id);
        return R.ok(num);
    }

    @PostMapping("/information/{id}")
    @ApiOperation("查询详情页信息")
    public Result<List<CrmModelFiledVO>> information(@PathVariable("id") @ApiParam(name = "id", value = "id") Integer id) {
        List<CrmModelFiledVO> information = crmAgreementService.information(id);
        return R.ok(information);
    }

    @PostMapping("/updateInformation")
    @ApiOperation("基本信息保存修改")
    @SysLogHandler(behavior = BehaviorEnum.UPDATE)
    public Result updateInformation(@RequestBody CrmUpdateInformationBO updateInformationBO) {
        crmAgreementService.updateInformation(updateInformationBO);
        return R.ok();
    }

    /**
     * 合同签署链接
     *
     * @param contractNo
     * @return
     */
    @GetMapping("/checkContract")
    @ApiOperation("合同签署链接")
    public Result checkContract(@RequestParam("contractNo") String contractNo){
        Map<String, Object> result = crmAgreementService.checkContract(contractNo);
        return R.ok(result);
    }

    /**
     * 合同数量
     *
     * @param customerId
     * @return
     */
    @GetMapping("/contractNum")
    @ApiOperation("合同数量")
    public Result contractNum(@RequestParam("customerId") Long customerId,@RequestParam("customerType") String customerType){
        Map<String, Object> result = crmAgreementService.contractNum(customerId,customerType);
        return R.ok(result);
    }

    /**
     * 总量统计
     *
     * @return
     */
    @GetMapping("/contractStatis")
    @ApiOperation("总量统计")
    public Result contractStatis(){
        Map<String, Object> result = crmAgreementService.contractStatis();
        return R.ok(result);
    }


    @PostMapping("/queryAgreement")
    @ApiOperation("查询学员下协议")
    public Result<BasePage<Map<String, Object>>> queryAgreement(@RequestBody CrmAgreementPageBo pageEntity) {
        BasePage<Map<String, Object>> basePage = crmAgreementService.queryAgreement(pageEntity);
        return R.ok(basePage);
    }

    @GetMapping("/personAuth")
    @ApiOperation("个人三要素实名认证")
    public Result<Map<String, Object>> personAuth(@RequestParam("signUserName") String signUserName,@RequestParam("idCard")String idCard,@RequestParam("telephone") String telephone) {
        Map<String, Object> result = crmAgreementService.personAuth(signUserName,idCard,telephone);
        return R.ok(result);
    }

}
